package com.example.leetcode.prcatice;

public class Demo2 {


    // b表示模式串，m表示模式串的长度
    private static int[] getNexts(char[] b, int m) {
        int[] next = new int[m];
        next[0] = -1;
        int k = -1;
        for (int i = 1; i < m; ++i) {
            while (k != -1 && b[k + 1] != b[i]) {
                k = next[k];
            }
            if (b[k + 1] == b[i]) {
                ++k;
            }
            next[i] = k;
        }
        return next;
    }

    public static void main(String[] args) {
        char[] c = {'a','b','a','b','c'};
        int[] nexts = getNexts(c, 5);
        for (int i = 0;i<nexts.length;i++){
            System.out.println("nexts["+i+"] ="+nexts[i]);
        }
    }
}
